package com.fulihui.wgj.admin.api.qrcode;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.fulihui.common.builder.web.JsonResultBuilder;
import com.fulihui.common.result.web.PageView;
import com.fulihui.common.result.web.Result;
import com.fulihui.wgj.admin.api.base.BaseController;
import com.fulihui.wgj.admin.api.material.vo.MaterialSingleVO;
import com.fulihui.wgj.admin.api.qrcode.excelmodel.QrcodeStatExcelModel;
import com.fulihui.wgj.admin.api.qrcode.request.QrcodeCreateRequest;
import com.fulihui.wgj.admin.api.qrcode.request.QrcodeExportDetailRequest;
import com.fulihui.wgj.admin.api.qrcode.request.QrcodeQueryDetailRequest;
import com.fulihui.wgj.admin.api.qrcode.request.QrcodeQueryRequest;
import com.fulihui.wgj.admin.api.qrcode.request.QrcodeReplyDelRequest;
import com.fulihui.wgj.admin.api.qrcode.request.QrcodeReplyEditRequest;
import com.fulihui.wgj.admin.api.qrcode.request.QrcodeReplyGetRequest;
import com.fulihui.wgj.admin.api.qrcode.vo.QrcodeCreateVO;
import com.fulihui.wgj.admin.api.qrcode.vo.QrcodeStatDetailVO;
import com.fulihui.wgj.admin.api.qrcode.vo.QrcodeStatVO;
import com.fulihui.wgj.admin.integration.ParamQrcodeServiceClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

/**
 * @author: Levon
 * @date: 2018-07-09 13:56
 */
@RestController
@RequestMapping("/qrcode")
@Api(tags = "参数二维码相关接口")
public class QrcodeManageController extends BaseController {

    private final Logger LOGGER = LoggerFactory.getLogger(QrcodeManageController.class);

    @Autowired
    private ParamQrcodeServiceClient paramQrcodeServiceClient;

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ApiOperation("创建参数二维码")
    public Result<QrcodeCreateVO> createQrcode(@RequestBody QrcodeCreateRequest createRequest, BindingResult validResult,
                                               HttpServletRequest request) {
        Result valid = valid(validResult);
        if (Objects.nonNull(valid)) {
            return valid;
        }

        createRequest.setAppId(getAppid(request));

        return paramQrcodeServiceClient.saveParamQrcodeInfo(createRequest);
    }

    @PostMapping("/list")
    @ApiOperation("获取参数二维码列表")
    public Result<PageView<QrcodeStatVO>> queryQrcodeList(@RequestBody @Valid QrcodeQueryRequest request, BindingResult validResult,
                                                          HttpServletRequest servletRequest) {
        Result valid = valid(validResult);
        if (Objects.nonNull(valid)) {
            return valid;
        }
        request.setAppId(getAppid(servletRequest));
        PageView<QrcodeStatVO> pageView = paramQrcodeServiceClient.queryPage(request);
        return JsonResultBuilder.succ(pageView);
    }


    @PostMapping("/detail")
    @ApiOperation("获取参数二维码统计详情")
    public Result<PageView<QrcodeStatDetailVO>> queryQrcodeDetail(@RequestBody @Valid QrcodeQueryDetailRequest request, BindingResult validResult,
                                                                  HttpServletRequest servletRequest) {
        Result valid = valid(validResult);
        if (Objects.nonNull(valid)) {
            return valid;
        }
        request.setAppId(getAppid(servletRequest));
        PageView<QrcodeStatDetailVO> pageView = paramQrcodeServiceClient.queryDetailPage(request);
        return JsonResultBuilder.succ(pageView);
    }

    @RequestMapping(value = "/export", method = RequestMethod.GET)
    @ApiOperation("导出参数二维码统计详情数据")
    public void export(QrcodeExportDetailRequest request,
                       HttpServletRequest servletRequest, HttpServletResponse response) {

        response.setHeader("content-Type", "application/vnd.ms-excel;charset=UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=qrcodeStat.xls");

        List<QrcodeStatExcelModel> excelModels = paramQrcodeServiceClient.exportDetail(request);
        ExportParams exportParams = new ExportParams("参数二维码统计详情", "sheet", ExcelType.HSSF);
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, QrcodeStatExcelModel.class, excelModels);
        try {
            workbook.write(response.getOutputStream());
        } catch (IOException e) {
            LOGGER.error("导出参数二维码统计详情数据出现错误", e);
        }
    }

    @PostMapping("/get")
    @ApiOperation("根据回复id获取素材详情")
    public Result<MaterialSingleVO> queryNewsList(@RequestBody @Valid QrcodeReplyGetRequest request, BindingResult validResult,
                                                  HttpServletRequest servletRequest) {
        Result valid = valid(validResult);
        if (Objects.nonNull(valid)) {
            return valid;
        }
        request.setAppId(getAppid(servletRequest));
        return paramQrcodeServiceClient.queryReplyById(request);
    }

    @PostMapping("/saveOrUpdate")
    @ApiOperation("修改/设置回复内容")
    public Result<Boolean> saveOrUpdate(@RequestBody @Valid QrcodeReplyEditRequest request, BindingResult validResult,
                                        HttpServletRequest servletRequest) {
        Result valid = valid(validResult);
        if (Objects.nonNull(valid)) {
            return valid;
        }
        request.setAppId(getAppid(servletRequest));
        return paramQrcodeServiceClient.saveOrUpdate(request);
    }

    @PostMapping("/del")
    @ApiOperation("删除回复内容")
    public Result<Boolean> del(@RequestBody @Valid QrcodeReplyDelRequest request, BindingResult validResult,
                               HttpServletRequest servletRequest) {
        Result valid = valid(validResult);
        if (Objects.nonNull(valid)) {
            return valid;
        }
        request.setAppId(getAppid(servletRequest));
        return paramQrcodeServiceClient.delReply(request);
    }

}
